import Vue from 'vue'
import App from './App'
import router from './router'
import api from './api/index'
import i18n from './i18n'

import store from './store/store.js'
import moment from 'moment'
import * as util from './common/util'
import * as check from './common/check'
import * as cache from './common/cache'
import reg from './common/reg'
import dict from '@/common/const/dict'
import md5 from 'js-md5'
import * as format from './common/format'
import rule from '@/common/const/rule'
import * as echarts from 'echarts'
import * as jsencrypt from './common/jsencrypt'

import ElementUI from 'element-ui'
// 自定义主题样式
import './assets/element-ui-theme/theme/index.css'

Vue.use(ElementUI)

import {
  openLoading,
  closeLoading
} from '@/common/loading'

import pageHeader from './components/page-header.vue'
Vue.component(pageHeader.name, pageHeader)

import emptydata from './components/emptydata.vue'
Vue.component(emptydata.name, emptydata)

//全局混入
import share from './mixins/share'
Vue.mixin(share)

//省略提示
import eltooltip from './components/el_tooltip'
Vue.component(eltooltip.name, eltooltip)


// 挂载loading等组件
Vue.prototype.$openLoading = openLoading
Vue.prototype.$closeLoading = closeLoading
Vue.prototype.$echarts = echarts
Vue.prototype.$moment = moment
Vue.prototype.$md5 = md5
Vue.prototype.$store = store
Vue.prototype.$cache = cache
Vue.prototype.$lang = i18n.t('m')
Vue.prototype.$util = util
Vue.prototype.$check = check
Vue.prototype.$dict = dict
Vue.prototype.$rule = rule
Vue.prototype.$reg = reg
Vue.prototype.$format = format
Vue.prototype.$jsencrypt = jsencrypt

Vue.prototype.$api = api
// 检查请求是否成功
Vue.prototype.$ok = res => res && res.code == '0'
// 国家信息
Vue.prototype.$country = {
  code: 'en',
  codes: 1,
  country: 'America'
}
Vue.prototype.$currency = {
  currency: '$',
  symbol: '$',
  country: 'America',
}
Vue.prototype.$dateTimePattern = 'YYYY-MM-DD  HH:mm:ss'
Vue.prototype.$datePattern = 'YYYY-MM-DD'

// 自定义指令
import './common/directives'

// 手机号脱敏
Vue.filter('maskedPhone', val => {
  if (val === null || val === '') {
    return ''
  }
  return val.substring(0, 2) + '****' + val.substr(-4)
})

// 邮箱脱敏
Vue.filter('maskedEmail', val => {
  if (val === null || val === '') {
    return ''
  }
  return val.split('@')[0].substring(0, 3) + '****' + '@' + val.split('@')[1]
})

// 语言列表
Vue.prototype.$languageList = [  {
    value: 'VI',
    lang: 'vi',
    maplang: 'vi-VN',
    label: 'Tiếng Việt',
    src: require('@/assets/imgs/language/VI.svg'),
  },
  {
    value: 'CN',
    lang: 'zh',
    maplang: 'zh-CN',
    label: '中文',
    src: require('@/assets/imgs/language/CN.svg'),
  },
  {
    value: 'EN',
    lang: 'en',
    maplang: 'en-US',
    label: 'English',
    src: require('@/assets/imgs/language/EN.svg'),
  }
]

// 日期格式化
Vue.filter('dateformat2', (val, type) => {
  let pattern
  type == 1 ? (pattern = 'HH:mm') : (pattern = 'MM-DD')
  return val ? moment(val).format(pattern) : ''
})

// 时长格式化
Vue.filter('formatSeconds', val => {
  return format.formatSeconds(val)
})

// 语言格式化
Vue.filter('langFormat', val => {
  return format.langFormat(val)
})

// 字符串格式化
Vue.filter('strFormat', val => {
  return format.strFormat(val)
})

// 数字格式化
Vue.filter('numberFormat', val => {
  return format.numberFormat(val)
})

// 金额格式化
Vue.filter('amountFormat', amount => {
  return format.amountFormat(amount)
})

// 状态格式化
Vue.filter('stateFormat', (val, list) => {
  return format.stateFormat(val, list)
})

// 百分比格式化
Vue.filter('ratioFormat', (val, total) => {
  return format.ratioFormat(val, total)
})

// 日期格式化
Vue.filter('dateformat', (val, pattern = Vue.prototype.$dateTimePattern) => {
  return format.dateformat(val, pattern)
})

// 日期范围格式化
Vue.filter(
  'dateRangeFormat',
  (start, end, pattern = Vue.prototype.$datePattern) => {
    return format.dateRangeFormat(start, end, pattern)
  }
)

// 手机号格式化
Vue.filter('phoneFormat', (val, code = '') => {
  return format.phoneFormat(val, code)
})

//JSON时间格式化
Vue.filter('timeFormats', val => {
  return format.timeFormats(val)
})

// uid格式化
Vue.filter('uidFormat', val => {
  return format.uidFormat(val)
})

// 状态格式化 / 颜色格式化
Vue.filter('valFormat', (val, list, color) => {
  if (val === null || val === '') {
    return ''
  }
  const find = (list || []).find(item => {
    if (item.value === null || item.value === '') {
      return false
    }
    return Number(item.value) === Number(val)
  })
  return find ? (color ? find.color : find.label) : ''
})

/** 格式化日期时间
 *对Date的扩展，将 Date 转化为指定格式的String
 *月(M)、日(d)、小时(h)、分(m)、秒(s)、季度(q) 可以用 1-2 个占位符，
 *年(y)可以用 1-4 个占位符，毫秒(S)只能用 1 个占位符(是 1-3 位的数字)
 */
Date.prototype.Format = function (fmt) {
  function getYearWeek(date) {
    var date1 = new Date(date.getFullYear(), date.getMonth(), date.getDate())
    var date2 = new Date(date.getFullYear(), 0, 1)

    //获取1月1号星期（以周一为第一天，0周一~6周日）
    var dateWeekNum = date2.getDay() - 1
    if (dateWeekNum < 0) {
      dateWeekNum = 6
    }
    if (dateWeekNum < 4) {
      //前移日期
      date2.setDate(date2.getDate() - dateWeekNum)
    } else {
      //后移日期
      date2.setDate(date2.getDate() + 7 - dateWeekNum)
    }
    var d = Math.round((date1.valueOf() - date2.valueOf()) / 86400000)
    if (d < 0) {
      var date3 = new Date(date1.getFullYear() - 1, 11, 31)
      return getYearWeek(date3)
    } else {
      //得到年数周数
      date1.getFullYear()
      const week = Math.ceil((d + 1) / 7)
      return week
    }
  }

  var o = {
    'M+': this.getMonth() + 1, //月份
    'd+': this.getDate(), //日
    'h+': this.getHours(), //小时
    'm+': this.getMinutes(), //分
    's+': this.getSeconds(), //秒
    'q+': Math.floor((this.getMonth() + 3) / 3), //季度
    S: this.getMilliseconds(), //毫秒
    'W+': getYearWeek(this), //周数
  }
  if (/(y+)/.test(fmt))
    fmt = fmt.replace(
      RegExp.$1,
      (this.getFullYear() + '').substr(4 - RegExp.$1.length)
    )
  for (var k in o)
    if (new RegExp('(' + k + ')').test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length == 1 ? o[k] : ('00' + o[k]).substr(('' + o[k]).length)
      )
    }
  return fmt
}

Vue.config.productionTip = false

new Vue({
  render: h => h(App),
  router,
  i18n,
  store,
}).$mount('#app')